package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import Function.FCMutilPlayCache;
import VWorldGameStudio.*;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Vector;

import static FCRendering.FCRMaster.*;

//售出
public class FunctionBuyOutShop extends AShopFuntion {
    private Vector<FixTank> needServiceNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;

    FixTank selectFixTank = null;

    public FunctionBuyOutShop() {
        buildShopWindow();
    }

    public void buildShopWindow() {
        for (int i = 0; i < 10; i++) {
            if (player1.getKnapsack()[i] != null && !player1.getKnapsack()[i].equals("null")) {
                needServiceNames.add(new FixTank(null, player1, player1.getKnapsack()[i], i, false));
            }
        }

        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null) {
            for (int i = 0; i < 10; i++) {
                if (tank.getKnapsack()[i] != null && !tank.getKnapsack()[i].equals("null")) {
                    needServiceNames.add(new FixTank(tank, player1, tank.getKnapsack()[i], i, true));
                }
            }
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                for (int i = 0; i < 10; i++) {
                    if (p.getKnapsack()[i] != null && !p.getKnapsack()[i].equals("null")) {
                        needServiceNames.add(new FixTank(null, p, p.getKnapsack()[i], i, false));
                    }
                }
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null) {
                    for (int i = 0; i < 10; i++) {
                        if (tank.getKnapsack()[i] != null && !tank.getKnapsack()[i].equals("null")) {
                            needServiceNames.add(new FixTank(tank, p, tank.getKnapsack()[i], i, true));
                        }
                    }
                }
            }
        }

        for (FCPropulsion fc : fcv.getPropulsions()) {
            if (fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")) {
                tank = (FCTank) fc;
                for (int i = 0; i < 10; i++) {
                    if (tank.getKnapsack()[i] != null && !tank.getKnapsack()[i].equals("null")) {
                        needServiceNames.add(new FixTank(tank, null, tank.getKnapsack()[i], i, true));
                    }
                }
            }
        }
        showFristIndex = 0;
        setEndI(Math.min(needServiceNames.size(), showFristIndex + 7));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        int m = 0;
        try {
            for (int i = showFristIndex; i < endI; i++) {
                FixTank fixTank = needServiceNames.get(i);
                FCItem item = FCRMaster.itemLib.getItemOfID(fixTank.itemName);
                if (item != null) {
                    FCRValues.leftWindowUI[m].leftText = item.getChName();
                    FCRValues.leftWindowUI[m].rightText = item.getSellOutPrice() + "G";
                    m++;
                }
            }
            if (selectFixTank != null) {
                FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
                FCRValues.rightWindowUI[0].leftText = "选择出售" + item.getChName();
            }
            FCRValues.rightWindowUI[3].rightText = "上一页";
            FCRValues.rightWindowUI[4].rightText = "下一页";
            FCRValues.rightWindowUI[5].rightText = "出售";
            FCRValues.rightWindowUI[6].rightText = "退出";
        }catch (Exception e){e.printStackTrace();}
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 1) {
            selectFixTank = needServiceNames.get(showFristIndex + i);
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
            FCRValues.rightWindowUI[0].leftText = "选择出售" + item.getChName();
        }
    }


    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 3:
                if (showFristIndex >= 7) {
                    showFristIndex -= 7;
                    selectFixTank = null;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 4:
                if (endI < needServiceNames.size()) {
                    showFristIndex += 7;
                    selectFixTank = null;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 5:
                if (selectFixTank == null) {
                    return;
                }
                FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
                player1.setMoney(player1.getMoney()+item.getSellOutPrice());
                if(selectFixTank.isTankBag){
                    selectFixTank.tank.insertItemToKnapsack(selectFixTank.witchKnapsack, "null");
                    selectFixTank.tank.reInitShuxing();
                }else {
                    selectFixTank.playerInfo.insertItemToKnapsack(selectFixTank.witchKnapsack, "null");
                }

                needServiceNames.remove(selectFixTank);
                selectFixTank = null;
                setEndI(Math.min(needServiceNames.size(), showFristIndex + 7));
                resetUI();
                break;
            case 6:
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }
}
